<template>
  <div>
    <div class="titletopXX">
      <van-nav-bar
        size="30"
        :title="title"
        :left-arrow="false"
        right-text=""
        :fixed="true"
      />
    </div>
    <van-search
      v-model="XXyz.districtName"
      placeholder="请输入搜索关键词"
      @search="onSearch"
      @clear="onCancel"
    />
    <van-pull-refresh
      class="pull-refresh_Search"
      v-model="refreshing"
      @refresh="onRefresh"
    >
      <van-empty
        v-if="list.length == 0 && !error"
        image="search"
        description="未找到数据"
      />
      <van-empty v-if="error" image="error" description="网络错误" />
      <van-list
        :immediate-check="false"
        v-model:loading="loading"
        :finished="finished"
        :error.sync="error"
        error-text="请求失败，点击重新加载"
        finished-text="没有更多了"
        @load="onLoad"
      >
        <!-- 列表内容 -->
        <div class="ulbox" v-if="list.length > 0">
          <div class="ul" v-for="(item, index) in list" :key="index">
            <div class="li" @click="BD(item)">
              <div class="top">
                <div class="WZ_size_20">
                  {{ item.districtName }}
                </div>
                <!-- <div class="WZ_size_12">{{ item.distance }}km</div> -->
              </div>
              <div class="WZ_size_12 marginTop12" style="color: #666">
                {{ item.location }}
              </div>
            </div>
          </div>
        </div>

        <!-- <div v-for="(item, index) in list" :key="index" class="item">

        {{ item.districtName }}
      </div> -->

        <!-- 自定义错误提示（可选增强样式） -->
        <template #error>
          <div class="error-tip" @click="retryLoad">
            <van-icon name="warning" />
            <span>加载失败，点击重试</span>
          </div>
        </template>
      </van-list>
    </van-pull-refresh>
  </div>
</template>

<script>
import titleXX from "../components/title.vue";
import { getpersonalInformation } from "../common/RYXX";
import {
  getPostAllList,
  selectUser,
  updatePost,
} from "@/api/realAssets/icsharedstation/home";
import { getandroidCode } from "../common/auth";
import { Toast } from "vant";
export default {
  components: { titleXX },
  data() {
    return {
      title: "驿站选择",
      XXyz: {
        postId: JSON.parse(getpersonalInformation()).postId,
        districtName: "",
        latitude: "", // 纬度
        longitude: "", // 经度
        pageNumber: 1,
        pageSize: 10,
      },
      finished: false,
      error: false,
      loading: false,
      refreshing: false,
      list: [],
    };
  },
  mounted() {
    this.onLoad();
  },
  methods: {
    //点击绑定
    BD(val) {
      //先获取手机号
      selectUser({
        ishid: getandroidCode(), //拿到编码
      }).then((resXX) => {
        updatePost({
          mobile: resXX.data.mobile,
          postId: val.postId,
        }).then((res) => {
          if (res.data == "绑定驿站完成" && res.resultMessage == "SUCCESS") {
            Toast.success(res.data);
            setTimeout(() => {
              //跳转到首页
              this.$router.replace({
                name: localStorage.getItem("routeName"),
              });
            }, 1000);
          }
        });
      });
    },
    onSearch() {
      this.onRefresh();
    },
    //清空搜索
    onCancel() {
      // console.log(XXyz.value.districtName);
      // XXyz.value.districtName = "";
      this.onRefresh();
    },
    retryLoad() {
      this.error = false;
      this.loading = true;
      this.onLoad();
    },
    onRefresh() {
      //   list.value = [];
      this.XXyz.pageNumber = 1;
      this.XXyz.pageSize = 10;
      this.finished = false;
      this.error = false;
      this.loading = true;
      this.refreshing = true;
      this.onLoad();
    },
    onLoad() {
      this.loading = true;

      if (this.error) return; // 如果当前是错误状态，不自动触发加载
      if (this.refreshing) {
        this.list = [];
      }
      this.XXyz.postId = JSON.parse(getpersonalInformation()).postId;
      getPostAllList(this.XXyz).then((res) => {
        if (res.resultMessage != "SUCCESS") {
          this.list = [];
          this.error = true; // 显示错误提示
          // loading.value = false;
        } else if (res.data.length == 0) {
          this.list = [];
          this.finished = true;
        } else {
          this.list = [...this.list, ...res.data];
          // list.value = res.data;
        }
        this.loading = false;
        // 判断是否已加载完所有数据
        if (this.list.length == res.pageNum) {
          this.finished = true;
        } else {
          this.XXyz.pageNumber++;
        }
        this.refreshing = false;
      });
    },
  },
};
</script>

<style lang="less" scoped>
@import "../static/css/index.less";
@import "../static/css/GYcSS.less";

.error-tip {
  padding: 16px;
  color: #f56c6c;
  text-align: center;
}
.error-tip .van-icon {
  margin-right: 4px;
  vertical-align: middle;
}
.item {
  padding: 12px 16px;
  /* border-bottom: 1px solid #f5f5f5; */
}
.li {
  height: auto !important;
  padding: 10px;
  box-sizing: border-box;
  display: block !important;

  .top {
    width: 100%;
    display: flex;
    justify-content: space-between;
    align-items: center;
  }
}
</style>
